//
//  DetailTableView.m
//  ZooNgonDoiOS
//
//  Created by Tran Minh Long on 19/7/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "DetailTableView.h"

@implementation DetailTableView
@synthesize _spotBean;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame style:UITableViewStylePlain];
    if (self) {
        self.delegate = self;
        self.dataSource = self;
        [self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_VIEW_BACKGROUND]]];
        [self setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        
        _arrComment = [[NSMutableArray alloc] initWithObjects:
                       @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
                       @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
                       @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                       @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", nil];
        
        // Delete later, it will load from spot bean
        _spotDescription = @"Đã bước đầu thành công trong việc tạo dựng nên một chuẩn mực mới cho ẩm thực cao cấp";
        
        _commentTextSize = CGSizeZero;
    }
    return self;
}

- (void) initMainImageWithCell: (UITableViewCell*) cell {
    
    // Main image
    _imgMainImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"takizawa.jpg"]];
    
    // Sub image
    // Sub image background
    UIImageView *_imgSubImageBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:IMG_SUB_IMAGE_BACKGROUND]];
    [_imgSubImageBackground setFrame:CGRectMake(0, cell.frame.size.height * 2 / 3 - 5, cell.frame.size.width, cell.bounds.size.height / 3)];

    [cell setBackgroundView:_imgMainImageView];
    
    [cell addSubview:_imgSubImageBackground];
    
    // Add sub image
    int x = 15;
    int y = _imgSubImageBackground.frame.origin.y + 5;
    UIImage *_imgSubImage = [UIImage imageNamed:@"takizawa.jpg"];
    for (int i = 0; i < 4; i++) {
        UIButton *_btnSubImage = [[UIButton alloc] initWithFrame:CGRectMake(x, y, _imgSubImageBackground.frame.size.height - 10, _imgSubImageBackground.frame.size.height - 10)];
        
        [_btnSubImage setBackgroundImage:_imgSubImage forState:UIControlStateNormal];
        
        [cell addSubview:_btnSubImage];
        
        [_btnSubImage release];
        
        x += _btnSubImage.frame.size.width + 15;
    }
    
    // Add button camera
    _btnCamera = [[UIButton alloc] initWithFrame:CGRectMake(_imgSubImageBackground.frame.size.width - _imgSubImageBackground.frame.size.height - 10, y - 5, _imgSubImageBackground.frame.size.height, _imgSubImageBackground.frame.size.height)];
    [_btnCamera setBackgroundImage:[UIImage imageNamed:IMG_CAMERA_NORMAL] forState:UIControlStateNormal];
    [_btnCamera setBackgroundImage:[UIImage imageNamed:IMG_CAMERA_SELECTED] forState:UIControlStateHighlighted];
    
    [cell addSubview:_btnCamera];
    
    [_imgMainImageView release];
    [_imgSubImageBackground release];
    [_btnCamera release];
}

- (void) initIntroductionWithCell: (UITableViewCell*) cell {
    
    // Init title
    UILabel *_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, cell.bounds.size.width - 20, 40)];

    [_titleLabel setBackgroundColor:[UIColor clearColor]];
    [_titleLabel setOpaque:YES];
    [_titleLabel setLineBreakMode:UILineBreakModeWordWrap];
    [_titleLabel setNumberOfLines:0];
    [_titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
    
    _titleLabel.text = @"Nhà hàng nướng & lẩu Nhật Bản SumoBBQ";
    
    // Init description
    UITextView *_descriptionLabel = [[UITextView alloc] initWithFrame:CGRectMake(5, _titleLabel.bounds.size.height, cell.bounds.size.width - 60, cell.bounds.size.height)];
    
    [_descriptionLabel setBackgroundColor:[UIColor clearColor]];
    [_descriptionLabel setFont:[UIFont systemFontOfSize:13]];
    [_descriptionLabel setEditable:NO];
    [_descriptionLabel setScrollEnabled:NO];
    
    _descriptionLabel.text = _spotDescription;
    
    _btnLocation = [[UIButton alloc] initWithFrame:CGRectMake(cell.bounds.size.width - 58, _titleLabel.bounds.size.height, 48, 48)];
    [_btnLocation setBackgroundImage:[UIImage imageNamed:IMG_MAP_NORMAL] forState:UIControlStateNormal];
    [_btnLocation setBackgroundImage:[UIImage imageNamed:IMG_MAP_SELECTED] forState:UIControlStateHighlighted];
    
    [cell.contentView addSubview:_titleLabel];
    [cell.contentView addSubview:_descriptionLabel];
    [cell.contentView addSubview:_btnLocation];
    
    [_titleLabel release];
    [_descriptionLabel release];
    [_btnLocation release];
}

- (void) initLikeWithCell: (UITableViewCell*) cell {
    [cell.contentView setBackgroundColor:[UIColor colorWithRed:200/255.0 green:217/255.0 blue:210/255.0 alpha:1]];
    
    // Add sub image
    int x = 5;
    int y = 10;
    UIImage *_imgSubImage = [UIImage imageNamed:@"takizawa.jpg"];
    
    for (int i = 0; i < 4; i++) {
        
        UIButton *_btnSubImage = [[UIButton alloc] initWithFrame:CGRectMake(x, y, cell.frame.size.height - 30, cell.frame.size.height - 30)];
        UILabel *_friendLike = [[UILabel alloc] initWithFrame:CGRectMake(0, _btnSubImage.frame.size.height, _btnSubImage.frame.size.width, 15)];
        
        [_btnSubImage setBackgroundImage:_imgSubImage forState:UIControlStateNormal];
        
        // Init friend like label
        [_friendLike setText:@"+ 15"];
        _friendLike.font = [UIFont boldSystemFontOfSize:14];
        _friendLike.textColor = [UIColor whiteColor];
        _friendLike.textAlignment = UITextAlignmentCenter;
        _friendLike.backgroundColor = [UIColor colorWithRed:46/255.0 green:129/255.0 blue:147/255.0 alpha:1];
        
        [cell addSubview:_btnSubImage];
        [_btnSubImage addSubview:_friendLike];
        
        [_friendLike release];
        [_btnSubImage release];
        
        x += _btnSubImage.frame.size.width + 15;
    }

}

/*
 * Add Comment table view to cell
 */
- (void) initCommentViewWithCell: (UITableViewCell*) cell forRowAtIndexPath: (NSIndexPath*) indexPath {
    if (indexPath.section == 3) {
        cell.textLabel.text = @"NHẬN XÉT";
        cell.textLabel.textAlignment = UITextAlignmentCenter;
        cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
        [cell.textLabel setTextColor:[UIColor colorWithRed:209/255.0 green:89/255.0 blue:71/255.0 alpha:1]];
    }
}

/*
 * Init comment to comment table view
 */
- (void) initComment: (UITableViewCell*) cell forRowAtIndexPath: (NSIndexPath *) indexPath{
    UIImageView *_imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"takizawa.jpg"]];
    _imageView.frame = CGRectMake(0, 0, 50, 50);
    
    // Init username
    UILabel *_lbUsername = [[UILabel alloc] initWithFrame:CGRectMake(55, 0, (cell.frame.size.width - _imageView.frame.size.width) / 2, 12)];
    _lbUsername.text = @"Takizawa1234567890";
    _lbUsername.backgroundColor = [UIColor clearColor];
    _lbUsername.numberOfLines = 1;
    _lbUsername.font = [UIFont boldSystemFontOfSize:12];
    
    // Init datetime label
    UILabel *_lbCommentDate = [[UILabel alloc] initWithFrame:CGRectMake(55, 0, cell.frame.size.width - _imageView.frame.size.width - 5, 12)];
    _lbCommentDate.textAlignment = UITextAlignmentRight;
    _lbCommentDate.backgroundColor = [UIColor clearColor];
    _lbCommentDate.numberOfLines = 1;
    _lbCommentDate.font = [UIFont systemFontOfSize:12];
    
    NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
    [_dateFormatter setDateFormat:@"[dd-MM-yyyy HH:mm]"];
    _lbCommentDate.text = [_dateFormatter stringFromDate:[NSDate date]];
    
    // Init comment label
    UITextView *_lbCommentText = [[UITextView alloc] initWithFrame:CGRectMake(50, 12, _commentTextSize.width, _commentTextSize.height)];
    _lbCommentText.font = [UIFont systemFontOfSize:12];
    _lbCommentText.backgroundColor = [UIColor clearColor];
    _lbCommentText.editable = NO;
    _lbCommentText.scrollEnabled = NO;
    _lbCommentText.text = [_arrComment objectAtIndex:indexPath.row];
    
    [cell addSubview:_imageView];
    [cell addSubview:_lbUsername];
    [cell addSubview:_lbCommentDate];
    [cell addSubview:_lbCommentText];

    [_imageView release];
    [_lbCommentText release];
    [_lbUsername release];
    [_lbCommentDate release];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 4) return [_arrComment count];
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier1 = @"cellIdentifier1";
    
    UITableViewCell *_cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
    
//    if (_cell == nil) {
        _cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier1] autorelease];
        [_cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//    }
    switch (indexPath.section) {
//        case 0:
//            [self initMainImageWithCell:_cell];
//            break;
//        case 1:
//            [self initIntroductionWithCell:_cell];
//            break;
//        case 2:
//            [self initLikeWithCell:_cell];
//            break;
//        case 3:
//            [self initCommentViewWithCell:_cell forRowAtIndexPath:indexPath];
//            break;
        case 4:
            [self initComment:_cell forRowAtIndexPath:indexPath];
            break;
    }
    return _cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)_cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        switch (indexPath.section) {
            case 0:
                [self initMainImageWithCell:_cell];
                break;
            case 1:
                [self initIntroductionWithCell:_cell];
                break;
            case 2:
                [self initLikeWithCell:_cell];
                break;
            case 3:
                [self initCommentViewWithCell:_cell forRowAtIndexPath:indexPath];
                break;
//            case 4:
//                [self initComment:_cell forRowAtIndexPath:indexPath];
//                break;
        }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.tag == IDX_DETAIL_TABLEVIEW) {
        switch (indexPath.section) {
            case 0:
                return self.bounds.size.height / 2.4;
            case 1:
            {
                CGSize _descriptionSize = [_spotDescription sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(260, 1000) lineBreakMode:UILineBreakModeWordWrap];
                
                return MAX(self.bounds.size.height / 6, _descriptionSize.height + 45);
            }
            case 2:
                return self.bounds.size.height / 6;
            case 3:
                return 30;
            case 4:
            {
                _commentTextSize = [(NSString*)[_arrComment objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(280, 1000) lineBreakMode:UILineBreakModeWordWrap];
                
                return MAX(self.bounds.size.height / 6 , _commentTextSize.height + 30);
            }
                
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0 || section == 3)
        return 0;

    return 10;
}

/*
 * Hide header view with no color
 */
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel *_clearHeaderColorLabel = [[UILabel alloc] init];
    _clearHeaderColorLabel.backgroundColor = [UIColor clearColor];
    
    return _clearHeaderColorLabel;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
